package com.ihighest.app.infox.activity;

import org.json.JSONException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.base.AbstractActivity;
import com.ihighest.app.common.core.Application;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.InformationActivity.ChangeBroadcast;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.zxing.CaptureActivity;

/**
 * 圈子界面
 * 
 * @author lkx Created at 2015-10-2 下午3:45:29
 * @version 1.0
 */
public class CircleActivity extends AbstractActivity implements OnClickListener {
	private ActivityTitle title;
	private PopupWindow mPopupWindow;
	private final int SCANER_CODE = 1;
	private BarCode receiveBroadCast;// 广播接收
	private ChangeBroadcast changeBroadcast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Application.getInstance().addActivity(this);
		setContentView(R.layout.infox_activity_circle);
		title = (ActivityTitle) findViewById(R.id.infox_cricleTitle);
		title.setTitle("圈子");
		title.setViewVersible(title.rightLayout, title.titleMsg);
		title.setViewGone(title.titleSetting, title.titleRefresh, title.titleSearch);

		changeBroadcast = new ChangeBroadcast();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Const.CHANGECOUNT);
		this.registerReceiver(changeBroadcast, filter);
		// 注册广播接收
		receiveBroadCast = new BarCode();
		IntentFilter filter2 = new IntentFilter();
		filter2.addAction(Const.SCREENBARCORD);
		registerReceiver(receiveBroadCast, filter2);
		initView();
		initPupoVindow();
		title.titleOther.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mPopupWindow != null && mPopupWindow.isShowing()) {
					mPopupWindow.dismiss();
				} else {
					showPupop(arg0);
				}

			}
		});
	}

	/**
	 * 初始化绑定控件
	 * 
	 * 
	 * @author lkx Created at 2015-10-5 上午10:58:41
	 */
	private void initView() {
		findViewById(R.id.infox_circle_groupChat).setOnClickListener(this);
		findViewById(R.id.infox_circle_buildingGroup).setOnClickListener(this);
		findViewById(R.id.infox_circle_expertOnline).setOnClickListener(this);
		findViewById(R.id.infox_circle_applyExpert).setOnClickListener(this);
		findViewById(R.id.infox_circle_Enterprise).setOnClickListener(this);
		findViewById(R.id.infox_circle_EnterpriseEntering).setOnClickListener(this);
		findViewById(R.id.infox_circle_selectColumn).setOnClickListener(this);
		findViewById(R.id.infox_circle_openColumn).setOnClickListener(this);

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.e("", "进入c");
		System.out.println(Const.UNREADMESSAGE);
		if (Const.UNREADMESSAGE != 0) {
			title.messagenumber.setVisibility(View.VISIBLE);
			title.messagenumber.setText(Const.UNREADMESSAGE + "");

		} else {
			title.messagenumber.setVisibility(View.GONE);
		}
	}

	/**
	 * 初始化popuwindow
	 * 
	 * 
	 * @author lkx Created at 2015-10-5 上午10:58:08
	 */
	private void initPupoVindow() {
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		View popupWindow = layoutInflater.inflate(R.layout.infox_popupwindow_layout, null);

		// 创建一个PopupWindow
		// 参数1：contentView 指定PopupWindow的内容
		// 参数2：width 指定PopupWindow的width
		// 参数3：height 指定PopupWindow的height
		mPopupWindow = new PopupWindow(popupWindow, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		// 必须设置背景
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		// 设置点击其他地方 就消失 (只设置这个，没有效果)

		mPopupWindow.setOutsideTouchable(true); /* 设置触摸外面时消失 */
		popupWindow.findViewById(R.id.pupopWindow_GroupChart).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Intent intent = new Intent(CircleActivity.this,
				// LaunchGroupChatActivity.class);
				// startActivity(intent);
				ToastKit.showShort(CircleActivity.this, Const.UN_FINISH_ALERT);

			}
		});
		popupWindow.findViewById(R.id.pupopWindow_richscan).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent openCameraIntent = new Intent(CircleActivity.this, CaptureActivity.class);
				startActivityForResult(openCameraIntent, SCANER_CODE);

			}
		});
	}

	private void showPupop(View v) {
		if (mPopupWindow == null) {
			initPupoVindow();
			mPopupWindow.showAsDropDown(v, 0, 20);
		} else {
			if (mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
			} else {
				mPopupWindow.showAsDropDown(v, 0, 20);
			}

		}

	}

	@Override
	public void onClick(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.infox_circle_groupChat:
//			 intent = new Intent(CircleActivity.this,
//			 CircleGroupActivity.class);
//			 startActivity(intent);
			ToastKit.showShort(CircleActivity.this, Const.UN_FINISH_ALERT);
			break;

		case R.id.infox_circle_buildingGroup:
//			 if
//			 (TextUtils.isEmpty(SharedPreferencesKit.getString(CircleActivity.this,
//			 Const.SP_KEY_USER_ID))) {
//			 intent = new Intent(CircleActivity.this, LoginActivity.class);
//			 startActivity(intent);
//			
//			 } else {
//			
//			 intent = new Intent(CircleActivity.this,
//			 LaunchGroupChatActivity.class);
//			 startActivity(intent);
//			 }
			ToastKit.showShort(CircleActivity.this, Const.UN_FINISH_ALERT);
			break;
		case R.id.infox_circle_expertOnline:
			intent = new Intent(CircleActivity.this, SpecialistOnlineActivity.class);
			startActivity(intent);
			break;
		case R.id.infox_circle_applyExpert:
			if (!TextUtils.isEmpty(SharedPreferencesKit.getString(CircleActivity.this, Const.SP_KEY_USER_ID))) {
				if (SharedPreferencesKit.getString(CircleActivity.this, Const.SP_KEY_USER_TYPE).equals("Specialist")) {
					ToastKit.showShort(CircleActivity.this, "您已经是专家");
				} else {

					applyTest("1", ApplyBeSpecialist.class);

				}
			} else {
				ToastKit.showShort(CircleActivity.this, Const.UN_LOGIN_ALERT);
			}

			break;
		case R.id.infox_circle_Enterprise:
			// intent = new Intent(CircleActivity.this,
			// EnterpriseNumberActivity.class);
			// startActivity(intent);
			ToastKit.showShort(CircleActivity.this, Const.UN_FINISH_ALERT);
			break;
		case R.id.infox_circle_EnterpriseEntering:
			// if   
			// (!TextUtils.isEmpty(SharedPreferencesKit.getString(CircleActivity.this,
			// Const.SP_KEY_USER_ID))) {
			//
			// applyTest("3", EnterpriseEnteringActivity.class);
			//
			// } else {
			// ToastKit.showShort(CircleActivity.this, Const.UN_LOGIN_ALERT);
			// }
			ToastKit.showShort(CircleActivity.this, Const.UN_FINISH_ALERT);
			break;
		case R.id.infox_circle_selectColumn:
			intent = new Intent(CircleActivity.this, SelectAColumnActivity.class);
			startActivity(intent);
			break;
		case R.id.infox_circle_openColumn:
			if (!TextUtils.isEmpty(SharedPreferencesKit.getString(CircleActivity.this, Const.SP_KEY_USER_ID))) {

				applyTest("2", ApplyForColumnActivity.class);

			} else {
				ToastKit.showShort(CircleActivity.this, Const.UN_LOGIN_ALERT);
			}
			break;

		}
	}

	/**
	 * 申请审核
	 * 
	 * 
	 * @author lkx Created at 2015-11-21 下午3:36:03
	 */
	private void applyTest(String type, final Class itent) {
		Api.get("apply/applyState", ApiParam.create().addParam("type", type), new ApiCallback(CircleActivity.this, false) {

			@Override
			public void handleSuccess(ApiResult result) {
				try {
					switch (result.getData().getInt("state")) {
					case 1:

						Intent intent = new Intent(CircleActivity.this, itent);
						startActivity(intent);
						break;

					case 2:
						ToastKit.showShort(getApplicationContext(), "您的申请正在审核");
						break;
					case 3:
						ToastKit.showShort(getApplicationContext(), "您的申请已经通过");
						break;
					case 4:
						ToastKit.showShort(getApplicationContext(), "您的申请未通过");
						break;
					case 5:
						ToastKit.showShort(getApplicationContext(), "对不起，您的等级还不能申请该项");
						break;
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			@Override
			public void handleFailure(Exception e, String msg) {
				// TODO Auto-generated method stub

			}
		}, CircleActivity.this);
	}

	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// super.onActivityResult(requestCode, resultCode, data);
	// if (requestCode == 1) {
	//
	// // Bundle bundle = data.getExtras();
	// // String scanResult = bundle.getString("result");
	// String scanResult = data.getStringExtra("result");
	//
	// ToastKit.showShort(CircleActivity.this, scanResult);
	//
	// }
	// }
	/**
	 * 二维码扫描结果广播
	 * 
	 * @author lkx Created at 2015-11-6 上午10:29:36
	 * @version 1.0
	 */
	public class BarCode extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 得到数据变化的广播
			if (intent.getAction().equals(Const.SCREENBARCORD)) {
				String stringExtra = intent.getStringExtra("result");
				// ToastKit.showShort(CircleActivity.this, stringExtra);
				// http://info.nzdb.com.cn/act/details?id=38ccbaff-318a-4be2-b3b3-b2e05349aaac&source=qr
				if (stringExtra.contains("http://info.nzdb.com.cn/act/details?id=")) {
					// 如果扫描结果包含这些内容
					String a = stringExtra.replace("http://info.nzdb.com.cn/act/details?id=", "");
					String[] strarray2 = a.split("&");

					String trueName = strarray2[0].toString();
					Intent DetailIntent = new Intent(CircleActivity.this, EventDetailActivity.class);
					DetailIntent.putExtra("id", trueName);
					startActivity(DetailIntent);

				}
			}

		}

	}

	public class ChangeBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 得到数据变化的广播
			if (intent.getAction().equals(Const.CHANGECOUNT)) {
				if (Const.UNREADMESSAGE != 0) {
					title.messagenumber.setText(Const.UNREADMESSAGE + "");
					title.messagenumber.setVisibility(View.VISIBLE);
				} else {
					title.messagenumber.setVisibility(View.GONE);
				}

			}

		}

	}

}
